package com.demo.steammanagement.common;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;

@Component
public class CommonUtils {

    @Value("${upload.base-url}")
    private String uploadBaseUrl;

    @Value("${upload.file-upload}")
    private String fileUpload;


    public String getUploadUrl(MultipartFile file) throws IOException {
        if (ObjectUtils.isEmpty(file)) {
            return null;
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取路径
        Path path = Paths.get(fileUpload,fileName);
        // 获取文件夹路径
        File filePath = new File(fileUpload);
        // 是否存在
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        // 复制文件
        Files.copy(file.getInputStream(),path, StandardCopyOption.REPLACE_EXISTING);
        return uploadBaseUrl + fileName;
    }

}
